package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.member.controller.admin.questionnaireinfo.vo.OptionInfo;
import com.xiaoshuidi.cloud.module.member.controller.admin.questionnaireinfo.vo.OptionItem;
import com.xiaoshuidi.cloud.module.member.mapper.questionnaireinfo.MemberOptionMapper;
import com.xiaoshuidi.cloud.module.member.pojo.questionnaireinfo.MemberOption;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberOptionRepository extends ServiceImpl<MemberOptionMapper, MemberOption> {
    private final MemberOptionMapper mapper;

    public Boolean batchCreate(List<OptionInfo> list) {
        List<MemberOption> memberOptions = new ArrayList<>();
        for (OptionInfo optionInfo : list) {
            for (OptionItem optionItem : optionInfo.getOptionItems()) {
                MemberOption memberOption = new MemberOption();
                BeanUtils.copyProperties(optionItem, memberOption);
                memberOption.setQuestionnaireId(optionInfo.getQuestionnaireId());
                memberOption.setQuestionId(optionInfo.getQuestionId());
                memberOptions.add(memberOption);
            }
        }
        //批量新增选项
        mapper.insertBatch(memberOptions);
        return true;
    }

    public List<MemberOption> getOptionsByQuestionnaireId(Long id) {
        LambdaQueryWrapper<MemberOption> wrapper = new LambdaQueryWrapper<MemberOption>().eq(MemberOption::getQuestionnaireId, id);
        return mapper.selectList(wrapper);
    }

    public Boolean removeBatchByQuestionnaireId(Long questionnaireId) {
        Map<String, Object> map = new HashMap<>();
        map.put("questionnaire_id", questionnaireId);
        mapper.deleteByMap(map);
        return true;
    }
}
